Imports System.Data
Imports System.Data.SqlClient
Partial Class Admin_frmModifyProduct
    Inherits System.Web.UI.Page
    Dim VarProductID As String
    Dim _ProductVO As New ProductVo
    Dim _BO As New Bo
    Dim _EmployeeId As String
    Dim _SchemeId As String
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        _EmployeeId = Session("EmpId")
        If _EmployeeId = "" Then
            Response.Redirect("../Client/frmLogin.aspx")
        End If
        If Not IsPostBack Then
            CategoryData()
            SubCategoryData()
            SchemeData()
        End If
        VarProductID = Request.QueryString("ProductId")
        If VarProductID = "" Then
            Response.Redirect("frmTabProduct.aspx")
        Else
            If Not IsPostBack Then
                GetProductData()
            End If
        End If
    End Sub
    Public Sub CategoryData()
        Dim dtCategory As DataTable
        dtCategory = _BO.getcategory()
        ddlCategoryId.DataSource = dtCategory
        ddlCategoryId.DataValueField = dtCategory.Columns(0).ToString
        ddlCategoryId.DataTextField = dtCategory.Columns(1).ToString
        ddlCategoryId.DataBind()
    End Sub
    Public Sub SubCategoryData()
        Dim dtSubCategory As DataTable
        dtSubCategory = _BO.getsubcategory(ddlCategoryId.SelectedValue)
        ddlSubCategoryId.DataSource = dtSubCategory
        ddlSubCategoryId.DataValueField = dtSubCategory.Columns(1).ToString
        ddlSubCategoryId.DataTextField = dtSubCategory.Columns(2).ToString
        ddlSubCategoryId.DataBind()
    End Sub
    Public Sub SchemeData()
        Dim dtScheme As DataTable
        dtScheme = _BO.getScheme
        ddlSchemeId.DataSource = dtScheme
        ddlSchemeId.DataValueField = dtScheme.Columns(1).ToString
        ddlSchemeId.DataTextField = dtScheme.Columns(2).ToString
        ddlSchemeId.DataBind()
    End Sub
    Sub GetProductData()
        _ProductVO = _BO.getProductById(VarProductID)
        lblProductId.Text = _ProductVO.ProductId
        ddlCategoryId.SelectedValue = _ProductVO.CategoryId
        ddlSubCategoryId.SelectedValue = _ProductVO.SubCategoryId
        txtPrice.Text = _ProductVO.Price
        txtBrand.Text = _ProductVO.Brand
        txtPhotoPath.Text = _ProductVO.PhotoPath
        'fuPhotoPath.Equals(_ProductVO.PhotoPath)
        ddlSchemeId.SelectedValue = _ProductVO.SchemeId
        txtTotalQuantity.Text = _ProductVO.TotalQuantity

        Dim varstr As String
        Dim varIndex As Integer
        varIndex = _ProductVO.PhotoPath.LastIndexOf("/")
        varstr = "~/Uploads" + _ProductVO.PhotoPath.Substring(varIndex)
        ImgPhoto.ImageUrl = varstr
    End Sub
    Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
        SetData()
        Session("EmpId") = _EmployeeId
    End Sub
    Sub SetData()
        If (fuPhotoPath.HasFile) Then
            fuPhotoPath.SaveAs(Server.MapPath("../Uploads/" & fuPhotoPath.FileName))
        End If
        Dim varPath As String
        _ProductVO.ProductId = lblProductId.Text
        _ProductVO.CategoryId = ddlCategoryId.SelectedValue
        _ProductVO.SubCategoryId = ddlSubCategoryId.SelectedValue
        _ProductVO.TotalQuantity = txtTotalQuantity.Text
        _ProductVO.Price = txtPrice.Text
        _ProductVO.Brand = txtBrand.Text
        If fuPhotoPath.FileName = "" Then
            varPath = "../Uploads/" + txtPhotoPath.Text
            _ProductVO.PhotoPath = varPath
        Else
            varPath = "../Uploads/" + fuPhotoPath.FileName
            _ProductVO.PhotoPath = varPath
        End If
        _ProductVO.SchemeId = ddlSchemeId.SelectedItem.ToString
        _BO.ModifyProduct(_ProductVO)
        ClearAll()
    End Sub
    Public Sub ClearAll()
        ddlCategoryId.SelectedIndex = 0
        ddlSubCategoryId.SelectedIndex = 0
        lblProductId.Text = ""
        txtPrice.Text = ""
        txtBrand.Text = ""
        fuPhotoPath.Equals("")

        MsgBox("Product Modified")
        Response.Redirect("frmTabProduct.aspx", False)
    End Sub
    Protected Sub ddlCategoryId_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlCategoryId.SelectedIndexChanged
        SubCategoryData()
    End Sub
    Protected Sub lnk1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnk1.Click
        fuPhotoPath.Visible = True
        txtPhotoPath.Visible = False
        lnk1.Visible = False
    End Sub

    Protected Sub btnReset_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnReset.Click
        lblProductId.Text = ""
        txtPrice.Text = ""
        txtBrand.Text = ""
        fuPhotoPath.Equals("")
    End Sub
End Class
